#!/usr/bin/env python
# encoding:utf-8
'''
@author: yangyongjie
@license: (C) Copyright 2013-2017, Node Supply Chain Manager Corporation Limited. 
@contact: deamoncao100@gmail.com
software: garner
@file: commodity.py
@time: 2022/8/27 21:07
@desc:
'''

from model import db
import datetime

class Commodities(db.Model):
    __tablename__ = "commodity"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    commodity_name = db.Column(db.String(128), nullable=False)
    detail = db.Column(db.String(256), nullable=False)
    images = db.Column(db.String(128), nullable=False)
    price = db.Column(db.Float(20), nullable=False)
    stocks = db.Column(db.Integer, nullable=False)
    create_time = db.Column(db.DateTime,  default=datetime.datetime.now())
    update_time = db.Column(db.DateTime,  default=datetime.datetime.now())

    category = db.relationship("Categories", backref="commodity")

    def keys(self):
        return ("id","commodity_name","detail","images","price","stocks","create_time","updata_tiem")

    def __getitem__(self, item):
        return getattr(self, item)
    def to_json(self):
        return {
            "commodity_name": self.commodity_name,
            "detail": self.detail,
            "images": self.images,
            "price":self.price,
            "stocks":self.stocks
        }


    @classmethod
    def create_commodity(cls, commodity_name, detail, images,  price, stocks):
        commodity = cls()
        commodity.commodity_name = commodity_name
        commodity.detail = detail
        commodity.images = images
        commodity.price = price
        commodity.stocks = stocks
        db.session.add(commodity)
        db.session.commit()


